package com.kakaxi.mybilibili.module.home.live.view;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.LiveHomeRecyAdapter;
import com.kakaxi.mybilibili.base.BaseFragment;
import com.kakaxi.mybilibili.bean.EventBusObject;
import com.kakaxi.mybilibili.bean.LiveAreaCategoryItemBean;
import com.kakaxi.mybilibili.bean.BannerItemBean;
import com.kakaxi.mybilibili.bean.LiveItemBean;
import com.kakaxi.mybilibili.bean.PartitionItemBean;
import com.kakaxi.mybilibili.bean.RecyMultiItemBean;
import com.kakaxi.mybilibili.module.all.WebViewActivity;
import com.kakaxi.mybilibili.module.home.live.presenter.ILiveHomePresenter;
import com.kakaxi.mybilibili.module.home.live.presenter.LiveHomePresenter;
import com.kakaxi.mybilibili.module.search.view.SearchActivity;
import com.kakaxi.mybilibili.utils.CastUncheckedUtil;
import com.kakaxi.mybilibili.utils.SliderLayoutHelper;
import com.kakaxi.mybilibili.view.GridMarginDecoration;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * 直播主页面碎片
 * Created by kakaxi on 16/8/24.
 */

public class LiveHomeFragment extends BaseFragment implements ILiveHomeView, LiveHomeRecyAdapter.onRecyclerViewClickListener {

    /**
     * 直播信息列表视图
     */
    private RecyclerView mLiveRecyView;

    /**
     * 直播列表适配器
     */
    private LiveHomeRecyAdapter mLiveAdapter;

    /**
     * 直播首页控制器
     */
    private ILiveHomePresenter mLivePresenter;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    /** 地区分类列表 **/
    private List<LiveAreaCategoryItemBean> mAreaCategoryItemBeanList;

    /** 是否跳转到全部分类活动标识 **/
    private boolean toAllCategoryActivity;

    public static LiveHomeFragment newInstance() {
        LiveHomeFragment liveHomeFragment = new LiveHomeFragment();
        return liveHomeFragment;
    }

    @Override
    public void onStart() {
        super.onStart();
        // 重置去向全部分类活动的标识
        toAllCategoryActivity = false;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_common_recycler;
    }

    @Override
    protected void setupViews(ViewGroup viewGroup) {
        // 初始化直播列表控件
        mLiveRecyView = (RecyclerView) viewGroup.findViewById(R.id.recv_common_fragment);
        mLiveRecyView.setHasFixedSize(true);
        // 初始化直播列表控件的布局管理器
        final GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (mLiveRecyView.getAdapter() != null) {
                    int type = mLiveRecyView.getAdapter().getItemViewType(position);
                    // 根据类型返回当前位置的数据占用的列数
                    if (type == LiveHomeRecyAdapter.VIEW_TYPE_CONTENT) {
                        return 1;
                    }
                }
                // 其他类型铺满整行
                return gridLayoutManager.getSpanCount();
            }
        });
        mLiveRecyView.addItemDecoration(new GridMarginDecoration(getContext(),
                GridMarginDecoration.DEFAULT_MARGIN_DP, LiveHomeRecyAdapter
                .VIEW_TYPE_CONTENT));
        mLiveRecyView.setLayoutManager(gridLayoutManager);

        // 初始化下拉刷新控件
        mSwipeRefreshLayout = (SwipeRefreshLayout) viewGroup.findViewById(R.id
                .srl_common_fragment);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mLivePresenter.refreshLiveData();
            }
        });
    }

    @Override
    protected void initData() {
        // 初始化直播控制器, 获取直播数据
        mLivePresenter = new LiveHomePresenter(this);
        initLiveData();
    }

    /**
     * 初始化直播数据
     */
    private void initLiveData() {
        setRefreshing(true);
        // 设置刷新并调用获取直播数据
        mLivePresenter.initLiveData();
    }

    /**
     * 设置刷新控件的刷新状态
     *
     * @param refreshing 是否正在刷新
     */
    private void setRefreshing(boolean refreshing) {
        mSwipeRefreshLayout.setRefreshing(refreshing);
    }

    @Override
    public void showLiveData(List<RecyMultiItemBean> liveData) {
        setRefreshing(false);
        if (mLiveAdapter == null) {
            mLiveAdapter = new LiveHomeRecyAdapter(getContext(), liveData);
            mLiveAdapter.setListener(this);
            mLiveRecyView.setAdapter(mLiveAdapter);
        } else {
            mLiveAdapter.setData(liveData);
            mLiveAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showLiveFailed(String message) {
        setRefreshing(false);
        toastHandler("获取数据失败:" + message);
    }

    @Override
    public void showLiveError() {
        setRefreshing(false);
        toastHandler("获取数据出错,请重试");
    }

    @Override
    public void setLiveAllCategoryData(List<LiveAreaCategoryItemBean> areaCategoryItemBeanList) {
        mAreaCategoryItemBeanList = areaCategoryItemBeanList;
    }

    @Override
    public void onStop() {
        super.onStop();
        // 当地区数据不为空并且去向标识正确时把数据发送过去
        if (mAreaCategoryItemBeanList != null && toAllCategoryActivity) {
            EventBusObject object = new EventBusObject(EventBusObject
                    .EVENT_LIVE_ALL_CATEGORY_ACT_SHOW_DATA, mAreaCategoryItemBeanList);
            EventBus.getDefault().post(object);
        }
    }

    @Override
    public void onBannerItemClick(View view, BaseSliderView baseSliderView) {
        // 获取对应点击滑动广告视图的数据
        RecyMultiItemBean recyMultiItemBean = (RecyMultiItemBean) view.getTag();
        List<BannerItemBean> bannerItemList = CastUncheckedUtil.cast(recyMultiItemBean.getData());
        String title = baseSliderView.getBundle().getString(SliderLayoutHelper.EXTRA_TITLE_TAG);
        BannerItemBean bannerItemBean = null;
        for (BannerItemBean itemBean : bannerItemList) {
            if (itemBean.getTitle().equals(title)) {
                bannerItemBean = itemBean;
                break;
            }
        }
        // 设置传递的数据并跳转到网页活动
        if (bannerItemBean != null) {
            Bundle bundle = new Bundle();
            bundle.putString(WebViewActivity.EXTRA_URL, bannerItemBean.getLink());
            goActivity(WebViewActivity.class, bundle);
        }
    }

    @Override
    public void onCategoryItemClick(View view, int column) {
        switch (column) {
            case LiveHomeRecyAdapter.COLUMN_CONSIDER_CATEGORY_ICON: // 关注主播
                goActivity(LiveConsiderActivity.class);
                break;
            case LiveHomeRecyAdapter.COLUMN_SEARCH_CATEGORY_ICON: // 搜索直播
                Bundle bundle = new Bundle();
                bundle.putInt(SearchActivity.EXTRA_TYPE, SearchActivity.TYPE_VIDEO);
                goActivity(SearchActivity.class, bundle);
                break;
            case LiveHomeRecyAdapter.COLUMN_ALL_CATEGORY_ICON: // 全部分类
                // 设置去向全部分类活动的标识
                toAllCategoryActivity = true;
                goActivity(LiveAllCategoryActivity.class);
                break;
        }
    }

    @Override
    public void onPartitionItemClick(View view) {
        // 传入分区直播间
        RecyMultiItemBean recyMultiItemBean = (RecyMultiItemBean) view.getTag();
        PartitionItemBean partitionItemBean = (PartitionItemBean) recyMultiItemBean.getData();
        Bundle bundle = new Bundle();
        bundle.putString(LiveCategoryRoomsActivity.EXTRA_TITLE, partitionItemBean.getName());
        bundle.putInt(LiveCategoryRoomsActivity.EXTRA_AREA_CATEGORY_ID, partitionItemBean.getId());
        goActivity(LiveCategoryRoomsActivity.class, bundle);
    }

    @Override
    public void onContentItemClick(View view) {
        // 传入直播间需要的数据,并跳转
        RecyMultiItemBean recyMultiItemBean = (RecyMultiItemBean) view.getTag();
        LiveItemBean liveItemBean = (LiveItemBean) recyMultiItemBean.getData();
        Bundle bundle = new Bundle();
        bundle.putInt(LiveRoomActivity.EXTRA_ROOM_ID, liveItemBean
                .getRoomId());
        bundle.putString(LiveRoomActivity.EXTRA_OWN_NAME, liveItemBean.getOwner().getName());
        bundle.putString(LiveRoomActivity.EXTRA_PLAY_URL, liveItemBean.getPlayurl());
        goActivity(LiveRoomActivity.class, bundle);
    }

}
